<?php

namespace App\Http\Controllers\Admin;

use App\Models\Node;
use Illuminate\Http\Request;

class NodeController extends BaseController
{
    /**
     * 显示
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if(request()->wantsJson()){
            //返回数组
            $data  = (new Node())->getAllLevel();
            return response()->json(['message' => '成功','count' => count($data),'code'=>0, 'data' => $data]);
        }else{
            return view('admin.node.index');
        }
    }

    /**
     * create
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = Node::where('pid', 0)->get();
        return view('admin.node.create', compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if(request()->wantsJson()){
            $this->validateData(0,$request);
            $re = Node::create($request->only('name','pid','route_name','is_menu'));
            if($re){
                return  $this->reSuJson('成功');
            }
            return $this->reErJson('失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Node  $node
     * @return \Illuminate\Http\Response
     */
    public function show(Node $node)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Node  $node
     * @return \Illuminate\Http\Response
     */
    public function edit(Node $node)
    {
        $data = Node::where('pid', 0)->get();
        return view('admin.node.edit', compact('data','node'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Node  $node
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Node $node)
    {
        $this->validateData($node->id,$request);
        $re = $node->update($request->only('name','pid','route_name','is_menu'));
        if($re){
            return $this->reSuJson('成功');
        }
        return $this->reErJson('失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Node  $node
     * @return \Illuminate\Http\Response
     */
    public function destroy(Node $node)
    {
        $re = $node->delete();
        if($re){
            return  $this->reSuJson('成功');
        }
        return  $this->reErJson('失败');
    }
    public function  validateData($id,Request $request){
        $name = 'required|unique:nodes,name';
        if($id){
            $name = $name.','.$id.',id';
        }
        $this->validate($request,['name'=>$name]);
//        try {
//        }catch (\Exception $e){
//            return $this->reErJson('权限名称已经存在');
//        }
    }
}
